﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class BuildManger : MonoBehaviour {

    public TurretData lasserTurretData;
    public TurretData MissleTurretData;
    public TurretData StandaraTurretData;

    private TurretData selectTurretData;
    //当前选择的炮台
    private MapCube selectedMapCube;
    //场景中的游戏物体

    public Text MoneyText;

    public Animator moneyAnimator;

    private int money = 1000;

    public GameObject upgradeCanvas;

    private Animator upgradeCanvasAnimator;

    public Button buttonUpgrade;

    void Start()
    {
        upgradeCanvasAnimator = upgradeCanvas.GetComponent<Animator>();
    
    }
    void ChangeMoney(int change = 0)
    {
        money += change;
        MoneyText.text = "￥" + money;
    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject() == false)
            { 
                //开发炮台建造
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                bool isCollider = Physics.Raycast(ray,out hit, 1000, LayerMask.GetMask("MapCube"));
                if (isCollider)
                {
                   MapCube MapCube = hit.collider.GetComponent<MapCube>();//得到点击的MapCube
                   if (selectTurretData != null && MapCube.TurretGo == null)
                   {
                       //可以创建
                       if (money > selectTurretData.cost)
                       {
                           ChangeMoney(-selectTurretData.cost);
                           MapCube.BuildTurret(selectTurretData);
                           
                       }
                       else 
                       { 
                         //提示钱不够
                           moneyAnimator.SetTrigger("flicker");
                       
                       }
                   }
                   else if (MapCube.TurretGo != null)
                   { 

                        //升级处理
                       
                       //if (MapCube.isUpgrade)
                       //{
                       //    ShowUpgradeUI(MapCube.transform.position, true);

                       //}
                       //else
                       //{
                       //    ShowUpgradeUI(MapCube.transform.position, false);
                       
                       //}
                       if (MapCube == selectedMapCube && upgradeCanvas.activeInHierarchy)
                       {
                           
                           StartCoroutine(HideUpgradeUI());
                       }
                       else {

                           ShowUpgradeUI(MapCube.transform.position, MapCube.isUpgrade);
                       }
                       selectedMapCube = MapCube;
                      
                   
                   }
                
                }
            
            }
        
        }
    
    
    }

    public void onLaserSeleted(bool isOn)
    {
        if (isOn)
        {
            selectTurretData = lasserTurretData;

        }


    }
    public void onMissleSeleted(bool isOn)
    {
        if (isOn)
        {
            selectTurretData = MissleTurretData;

        }
    }
    public void onStandardSeleted(bool isOn)
    {

        if (isOn)
        {
            selectTurretData = StandaraTurretData;

        }

    }
    void ShowUpgradeUI(Vector3 pos, bool isDisableUpgrade = false)
    {
        StopCoroutine("HideUpgradeUI");
        upgradeCanvas.SetActive(false);
        upgradeCanvas.SetActive(true);
        upgradeCanvas.transform.position = pos;
        buttonUpgrade.interactable = !isDisableUpgrade;
    
    }
    IEnumerator HideUpgradeUI()
    {
        upgradeCanvasAnimator.SetTrigger("Hide");
        
        yield return new WaitForSeconds(0.8f);
        upgradeCanvas.SetActive(false);
    }
    public void OnUpgradeButtonDown()
    {
        if (money >= selectedMapCube.turretData.costUpgraded)
        {
            ChangeMoney(-selectedMapCube.turretData.costUpgraded);
            selectedMapCube.UpgradeTurret();
        }
        else
        {
            moneyAnimator.SetTrigger("flicker");
        
        }
        StartCoroutine(HideUpgradeUI());
    
    }
    public void OnDestroyButtonDown()
    {
        selectedMapCube.DestroyTurret();
        StartCoroutine(HideUpgradeUI());
    
    }
}
